package com.linjiu.edit;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.inputmethod.EditorInfo;

import androidx.core.content.ContextCompat;


/**
 * Created by alice on 2018/10/12.
 */

public class EditTextWithClear extends androidx.appcompat.widget.AppCompatEditText {

    private Context mContext;

    private Drawable icLogo;
    private Drawable icClear;
    private Drawable icEye;
    private Drawable icMyClear;
    private boolean isShow = false;

    public void setIcClear(Drawable icClear) {
        this.icMyClear = icClear;
    }

    public void setIcEye(Drawable icEye) {
        this.icEye = icEye;
        this.setSingleLine();
        setEditStyle();
        setImage();
    }

    private void setEditStyle() {
        if (isShow) {
            this.setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_NORMAL);
            this.setSelection(this.getText().length());
        } else {
            this.setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
            this.setSelection(this.getText().length());
        }
    }

    public EditTextWithClear(Context context) {
        this(context, null);
    }

    public EditTextWithClear(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.editTextStyle);
    }

    public EditTextWithClear(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    public void setLeftLogo(Drawable icLogo) {
        this.icLogo = icLogo;
    }

    private void init() {
        icClear = getCompoundDrawables()[2];
        if (icClear == null) {
            icClear = ContextCompat.getDrawable(mContext, R.mipmap.btn_delete_right);
        }

        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                setImage();
            }
        });
        setImage();
    }

    private void setImage() {
        if (length() > 0) {
            //Log.e("高度", "H" + getHeight());
            int margin = ViewUtil.dp2px(mContext, 10);
            if (icEye != null) {
                int mH = (getHeight() - margin) * 2;
                //Log.e("高度","icEye mH"+icEye.getIntrinsicHeight());
                icEye = ViewUtil.zoomDrawable(icEye, mH, mH);
                //Log.e("高度","icEye mH change"+icEye.getIntrinsicHeight());
                setCompoundDrawablesWithIntrinsicBounds(icLogo, null, icEye, null);
            } else if (icMyClear != null) {
                int mH = (getHeight() - margin) * 2;
                icMyClear = ViewUtil.zoomDrawable(icMyClear, mH, mH);
                setCompoundDrawablesWithIntrinsicBounds(icLogo, null, icMyClear, null);
            } else {
                setCompoundDrawablesWithIntrinsicBounds(icLogo, null, icClear, null);
            }

            setCompoundDrawablePadding(margin);
        } else {
            setCompoundDrawablesWithIntrinsicBounds(icLogo, null, null, null);
            setCompoundDrawablePadding(0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (getCompoundDrawables()[2] != null && event.getAction() == MotionEvent.ACTION_UP) {
            //得到手指离开EditText时的X Y坐标
            int x = (int) event.getX();
            int y = (int) event.getY();
            Rect rect = getCompoundDrawables()[2].getBounds();
            int height = rect.height();
            int distance = (getHeight() - height) / 2;
            boolean isInnerWidth = x > (getWidth() - getTotalPaddingRight()) && x < (getWidth() - getPaddingRight());
            boolean isInnerHeight = y > distance && y < (distance + height);
            if (isInnerWidth && isInnerHeight) {
                if (icEye != null) {
                    isShow = !isShow;
                    setEditStyle();
                } else if (icMyClear != null) {
                    this.setText("");
                } else {
                    this.setText("");
                }
            }
        }
        return super.onTouchEvent(event);
    }

}
